package com.company.project.wechat.officialAccounts.controller;

import com.company.project.wechat.officialAccounts.service.WxOfficialAccountsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
  * @author ZYF
 * @date 2021/5/18 11:03
 */
@Api(tags = "微信公众号验证签名", description = "微信调用")
@RestController
@RequestMapping("/wxApi")
@Slf4j
public class WxServletControl {

    @Autowired
    WxOfficialAccountsService wxOfficialAccountsService;


    /**
     * <P>
     * 标题：微信服务器身份验证
     * </p>
     *
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @param request
     * @param response
     * @throws Exception
     */
    @ApiOperation(value = "微信服务器身份验证", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "signature", value = "微信加密签名", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "timestamp", value = "时间戳", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "nonce", value = "随机数", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "echostr", value = "随机字符串", dataType = "string", paramType = "query", required = true) })
    @RequestMapping(value = "/wxAuthTo", produces = { "application/json;charset=UTF-8" })
    public void wxAuthTo(String signature, String timestamp, String nonce, String echostr, HttpServletRequest request,
                           HttpServletResponse response) throws Exception {
        boolean isGet = request.getMethod().toLowerCase().equals("get");
        if (isGet) {
            log.info("--------------------------------------签名开始--------------------------------------");
            String validataCode = wxOfficialAccountsService.getValidateCode(signature, timestamp, nonce, echostr);
            response.getWriter().write(validataCode);
            log.info("--------------------------------------签名结束--------------------------------------");
        }else{
            wxOfficialAccountsService.acceptMessage(request,response);
        }
    }






}
